﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using MisDLL;
using System.Data;

namespace MisBLL
{
    public class FkTypeDAO : MarshalByRefObject, IFkType
    {
        protected DbHelper dbHelper = new DbHelper();

        protected MisEntity.DataEntityDataContext Ctx = new MisEntity.DataEntityDataContext();

        //根据编码查询客户结算类型信息
        public MisModel.FKType GetById(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            string sql = string.Format("select * from FKTYPE_TAB where FK_id='{0}'", id);
            DataTable dt=  dbHelper.ExecuteDataTable(sql);
            MisModel.FKType entity = null;
            if (dt != null && dt.Rows.Count > 0){
                entity = new MisModel.FKType();
                entity.FkID = dt.Rows[0]["FK_id"].ToString();
                entity.FkType = dt.Rows[0]["Fktype"].ToString();
                entity.F1 = dt.Rows[0]["F1"].ToString();
            }
            return entity;
        }

        //查询客户结算类型列表
        public DataTable GetAll()
        {
            string sql = "select FK_id,Fktype,F1,(FK_id + '.' + Fktype) as ItemShow  from FKTYPE_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        public IList<MisEntity.FKType> FindAll()
        {
            var query = from item in Ctx.FKType
                        select item;
            if (query.Count<MisEntity.FKType>() == 0)
                return null;
            return query.ToList<MisEntity.FKType>();
        }

        //添加客户结算类型信息
        public int Add(MisModel.FKType obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string sql = string.Format("insert into FKTYPE_TAB(FK_id,Fktype,F1) values('{0}','{1}','{2}')", obj.FkID, obj.FkType, obj.F1);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改客户结算类型信息
        public int Update(MisModel.FKType obj)
        {
            string sql = string.Format("update FKTYPE_TAB set Fktype='{0}',F1='{1}' where FK_id='{2}'", obj.FkType, obj.F1, obj.FkID);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除客户结算类型信息
        public int Delete(string id)
        {
            string sql = string.Format("delete from FKTYPE_TAB where FK_id='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }
    }
}
